//
//  YLProgressHUD.swift
//  test
//
//  Created by lyl on 2020/7/12.
//  Copyright © 2020年 lyl. All rights reserved.
//

import UIKit

class YLProgressHUD: UIView {

    /// 在指定的视图上等待加载...
    class func showWait(_ onView: UIView) {
        let waitView = YLProgressHUD(frame:CGRect(x: 0, y: 0, width: 78, height: 78))
        waitView.center = onView.center
        waitView.layer.cornerRadius = 12
        waitView.backgroundColor = UIColor(red:0, green:0, blue:0, alpha: 0.8)
        
        let ai = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.whiteLarge)
        ai.frame = CGRect(x: 21, y: 21, width: 36, height: 36)
        ai.startAnimating()
        waitView.addSubview(ai)
        
        onView.addSubview(waitView)
        onView.isUserInteractionEnabled = false
        
        waitView.alpha = 0.0
        UIView.animate(withDuration: 0.2, animations: {
            waitView.alpha = 1
        })
    }
    
    class func hideHUDForView(_ view: UIView) {
        DispatchQueue.main.async {
            view.isUserInteractionEnabled = true
            let subviews = view.subviews
            for subview in subviews {
                if subview is YLProgressHUD {
                    subview.removeFromSuperview()
                    return
                }
            }
        }
    }

}
